package edu.zut.cs.software.cms.module.attendance.web.spring.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.zut.cs.software.cms.base.web.spring.controller.GenericController;
import edu.zut.cs.software.cms.module.attendance.domain.Attendance;
import edu.zut.cs.software.cms.module.attendance.service.AttendanceManager;

/**
 * 
 * @author 清风依旧
 *
 */
@Controller
@RequestMapping("/module/attendance")
@ResponseBody
public class AttendanceController extends GenericController<Attendance , Long, AttendanceManager> {
	AttendanceManager attendanceManager;

	@RequestMapping(method = RequestMethod.GET, value = "/index.html")
	public String index() {
		String result = "/attendance/index";
		return result;
	}

	@Autowired
	public void setAttendanceManager(AttendanceManager attendanceManager) {
		this.attendanceManager = attendanceManager;
		this.manager = this.attendanceManager;
	}
	
	/*
	 * delete attendance
	 */
	@GetMapping("/deletebyid/{id}")
	public Boolean deleteById(@PathVariable("id") Long id) {
		Boolean result = this.manager.deleteById(id);
		return result;
	}
//	@RequestMapping("/deleteAttendance")
//	@ResponseBody
//	public String deleteAttendance(long id) {
//		if(attendanceManager.deleteById(id)) {
//			attendanceManager.delete(id);
//			return "delete success";			
//		}else
//			return "delete failure";
//	}
	
	/*
	 * modify attendance
	 */
	@RequestMapping("/modifyAttendance")
	@ResponseBody
	public String modifyAttendance(long id,String status) {
		if(attendanceManager.modifyById(id,status)==true)
			return "modify success";
		else
			return "modify failure";
	}
	
	/*
	 * add attendance
	 */
	@RequestMapping("/CreateAttendance")
	@ResponseBody
	public String CreateAttendance(String name,String code,String clas,String status,String time) {
		String flag="";
		Attendance attendance=new Attendance();
		if(attendanceManager.IfExist(name))//返回true
		{
			flag="true";
			attendance.setName(name);
			attendance.setCode(code);
			attendance.setClas(clas);
			attendance.setStatus(status);
			attendance.setTime(time);
			attendanceManager.save(attendance);
		}
		else {
			flag="false";
		}
		return flag;
	}

	/*
	 * search attendance
	 */
	@RequestMapping("/RetrieveAttendanceById")
	@ResponseBody
	public Attendance RetrieveById(long id) {
		Attendance attendance=new Attendance();
		attendance=attendanceManager.findById(id);
		return attendance;
	}
}